package com.piggy.logi.sys.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.service.ILogiSysOperLogService;
import com.piggy.logi.sys.api.domain.LogiSysOperLog;
import com.piggy.logi.sys.mapper.LogiSysOperLogMapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 操作日志 服务层处理
 *
 * @author shark
 */
@Service
public class LogiLogiSysOperLogServiceImpl extends ServiceImpl<LogiSysOperLogMapper, LogiSysOperLog> implements ILogiSysOperLogService {

    @Override
    public TableDataInfo<LogiSysOperLog> selectPageOperLogList(LogiSysOperLog operLog) {
        Map<String, Object> params = operLog.getParams();
        LambdaQueryWrapper<LogiSysOperLog> lqw = new LambdaQueryWrapper<LogiSysOperLog>()
                .like(StrUtil.isNotBlank(operLog.getTitle()), LogiSysOperLog::getTitle, operLog.getTitle())
                .eq(operLog.getBusinessType() != null && operLog.getBusinessType() > 0,
                        LogiSysOperLog::getBusinessType, operLog.getBusinessType())
                .func(f -> {
                    if (ArrayUtil.isNotEmpty(operLog.getBusinessTypes())) {
                        f.in(LogiSysOperLog::getBusinessType, Arrays.asList(operLog.getBusinessTypes()));
                    }
                })
                .eq(operLog.getStatus() != null && operLog.getStatus() > 0,
                        LogiSysOperLog::getStatus, operLog.getStatus())
                .like(StrUtil.isNotBlank(operLog.getOperName()), LogiSysOperLog::getOperName, operLog.getOperName())
                /*.apply(Validator.isNotEmpty(params.get("beginTime")),
                        "date_format(oper_time,'%y%m%d') >= date_format({0},'%y%m%d')",
                        params.get("beginTime"))
                .apply(Validator.isNotEmpty(params.get("endTime")),
                        "date_format(oper_time,'%y%m%d') <= date_format({0},'%y%m%d')",
                        params.get("endTime"))*/
                .apply(ObjectUtil.isAllNotEmpty(operLog.getCreateTimeStart(), operLog.getCreateTimeEnd()), "date_format(oper_time,'%y%m%d') between date_format({0},'%y%m%d') and date_format({1},'%y%m%d') " , operLog.getCreateTimeStart(), operLog.getCreateTimeEnd())
                .orderByDesc(LogiSysOperLog::getOperId);
        return PageUtils.buildDataInfo(page(PageUtils.buildPage(), lqw));
    }

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public boolean insertOperlog(LogiSysOperLog operLog) {
        operLog.setOperTime(new Date());
        return  save(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<LogiSysOperLog> selectOperLogList(LogiSysOperLog operLog) {
        Map<String, Object> params = operLog.getParams();
        return list(new LambdaQueryWrapper<LogiSysOperLog>()
                .eq(LogiSysOperLog::getMainDeptId, operLog.getMainDeptId())
                .like(StrUtil.isNotBlank(operLog.getTitle()), LogiSysOperLog::getTitle,operLog.getTitle())
                .eq(operLog.getBusinessType() != null && operLog.getBusinessType() > 0,
                        LogiSysOperLog::getBusinessType,operLog.getBusinessType())
                .func(f -> {
                    if (ArrayUtil.isNotEmpty(operLog.getBusinessTypes())){
                        f.in(LogiSysOperLog::getBusinessType, Arrays.asList(operLog.getBusinessTypes()));
                    }
                })
                .eq(operLog.getStatus() != null && operLog.getStatus() > 0,
                        LogiSysOperLog::getStatus,operLog.getStatus())
                .like(StrUtil.isNotBlank(operLog.getOperName()), LogiSysOperLog::getOperName,operLog.getOperName())
                /*.apply(Validator.isNotEmpty(params.get("beginTime")),
                        "date_format(oper_time,'%y%m%d') >= date_format({0},'%y%m%d')",
                        params.get("beginTime"))
                .apply(Validator.isNotEmpty(params.get("endTime")),
                        "date_format(oper_time,'%y%m%d') <= date_format({0},'%y%m%d')",
                        params.get("endTime"))*/
                .apply(ObjectUtil.isAllNotEmpty(operLog.getCreateTimeStart(), operLog.getCreateTimeEnd()), "date_format(oper_time,'%y%m%d') between date_format({0},'%y%m%d') and date_format({1},'%y%m%d') " , operLog.getCreateTimeStart(), operLog.getCreateTimeEnd())
                .orderByDesc(LogiSysOperLog::getOperId));
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(operIds));
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public LogiSysOperLog selectOperLogById(Long operId) {
        return getById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        remove(new LambdaQueryWrapper<>());
    }
}
